package cn.com.fssg.platform.base.handler.impl.pjlc;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 评价流程记录状态更新接口
 */
@Component
public class PjlcStatusCtl implements InterfaceHandler {

//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String ids = jsonObject.get("ids").toString();
        List<String> idList = Arrays.asList(ids.split(","));
        String type = jsonObject.get("type").toString();
        boolean isPass = jsonObject.getBoolean("isPass");
        String sql = "SELECT * FROM bs_qyxyxwdj where id in (:ids)";
        if (!type.equals("qy")) {
            // 不是企业行为登记就是关键人员评价
            sql = "SELECT * FROM bs_ryxyxwdj where id in (:ids)";
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("ids", idList);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, params);
        for (Map<String, Object> map : maps) {
            String id = map.get("id").toString();
            String zt = map.get("zt").toString();
            String nextZt = getNextStatus(zt, isPass);
            String jd = getNowJd(zt);
            if (!"qy".equals(type)) {
                // 不是企业行为登记就是关键人员评价
                 sql = "update bs_ryxyxwdj set zt = :nextZt where id = :id";
            } else {
                sql = "update bs_qyxyxwdj set zt = :nextZt where id = :id";
            }

            params.clear();
            params.put("nextZt", nextZt);
            params.put("id", id);
            // 更新行为登记表的状态
            jdbcTemplate.update(sql, params);
            // 生成日志记录表
            String insertSql = "INSERT INTO bs_pjlc_log (id, operate, parentid, shsj, jd, createid, createname, lx) VALUES (:id, :operate, :parentid, :shsj, :jd, :createid, :createname, :lx)";
            params.clear();
            params.put("id", BizUtil.cur().getSnowflakeIdStr());
            params.put("operate", isPass ? nextZt : "退回");
            params.put("parentid", id);
            params.put("shsj", new Date());
            params.put("jd", jd);
            params.put("createid", BizUtil.cur().curUserId());
            params.put("createname", BizUtil.cur().curLoginname());
            params.put("lx", type);
            jdbcTemplate.update(insertSql, params);
        }
        return null;
    }

    private String getNowJd(String zt) {
        switch (zt) {
            case "已确认":
                return "公路学会初审";
            case "初审通过":
                return "各单位审查";
            case "审查通过":
                return "公路学会终审";
            default:
                return "未知阶段";
        }
    }

    private String getNextStatus(String zt, boolean isPass) {
        switch (zt) {
            case "已确认":
                return isPass ? "初审通过" : "已退回";
            case "初审通过":
                return isPass ? "审查通过" : "已退回";
            case "审查通过":
                return isPass ? "终审通过" : "已退回";
            default:
                return "已退回";
        }
    }
}
